package org.vuzya.motivateme.models;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

public class Businesses implements BaseColumns {
	public static final String TABLE_NAME = "Businesses";
	public static final String TITLE = "Title";
	public static final String DESCRIPTION = "Description";
	public static final String START_DATE = "StartDate";
	public static final String FINISH_DATE = "FinishDate";
	public static final String[] SELECTION = { _ID, TITLE, DESCRIPTION, START_DATE, FINISH_DATE };
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat();
	
	private long id = -1;
	private String title;
	private String description;
	private Date startDate;
	private Date finishDate;

	public Businesses(){
		startDate=new Date();
		finishDate=null;
	}
	
	public Businesses(Cursor cursor){
		id = cursor.getLong(cursor.getColumnIndex(_ID));
		title = cursor.getString(cursor.getColumnIndex(TITLE));
		description = cursor.getString(cursor.getColumnIndex(DESCRIPTION));
		
		String tmp = "";
		try {
			tmp = cursor.getString(cursor.getColumnIndex(START_DATE));
			if(tmp!=null){
				startDate = sdf.parse(tmp);
			}else{
				startDate = null;
			}
			
			tmp = cursor.getString(cursor.getColumnIndex(FINISH_DATE));
			if(tmp!=null){
				finishDate = sdf.parse(tmp);
			}else{
				finishDate = null;
			}
		} catch (ParseException e) {
			Log.d(TABLE_NAME, "Wrong date format: " + tmp);
		}
	}
	
	public ContentValues extractValues(){
		ContentValues cv = new ContentValues();
		if(id!=-1){
			cv.put(_ID, id);
		}
		cv.put(DESCRIPTION, description);
		cv.put(TITLE, title);
		cv.put(START_DATE, startDate.toString());
		cv.put(FINISH_DATE, finishDate.toString());
		return cv;
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getFinishDate() {
		return finishDate;
	}
	public void setFinishDate(Date finishDate) {
		this.finishDate = finishDate;
	}
	public long getId() {
		return id;
	}
}
